
import re
import string
from pepe.txt import bigtxt

#---------------------------------------------------------
def cols_reordered_file(infile_s, outfile_s, corder_s, cdelimiter_s):
    """
    i: csv file, string with requested column order, csv column delimiter
    o: reordered csv file
    e:
    from pepe.csv import cols_reordered_file as crf
    crf(a'csv', 'b.csv', '3-2-1', '|')  
    """
    out_fl = open(outfile_s,'w')
    order_l = string.split(corder_s,'-')
    for line in bigtxt.lines(infile_s):
        line = re.sub('\n','',line)
        res = cols_reordered_string(line,order_l,cdelimiter_s)
        out_fl.write(res+'\n')
    return 1
    
#---------------------------------------------------------
def cols_reordered_string(in_s, order_l, cold_s):
    """
    i: csv string, requested column order, csv column delimiter
    o: reordered csv string
    e:
    from pepe.csv import cols_reordered_string as crs
    crs('<line_s>','2-1-0','|')
    """
    res = ''
    cols_l = string.split(in_s, cold_s)
    for cindex in order_l:
        res += cols_l[int(cindex)]+cold_s
    return res

def main():
    cols_reordered_file('test.csv','test_.csv','2-1-0',';')

if __name__ == "__main__":
    main()
